package com.power.sifa.correction.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.power.sifa.correction.model.Cre_warning_detail;
import com.power.sifa.correction.model.pageInfo.Cre_warning_detailPageInfo;
import com.power.sifa.correction.api.ICre_warning_detailFeign;
import com.power.sifa.correction.service.Cre_warning_detailService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   预警信息违规详情控制器
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Cre_warning_detailController",tags={"预警信息违规详情"})
public class Cre_warning_detailController implements ICre_warning_detailFeign{

    @Resource
    private Cre_warning_detailService cre_warning_detailService;


    @Override
    public ResponseDTO queryListByWarnid(String warnid) {
        try {
            return ResponseDTO.succData(this.cre_warning_detailService.queryListByWarnid(warnid));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("/cre_warning_detail/queryListByWarnid 根据预警id查询明细失败 ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, "根据预警id查询明细失败");
        }

    }

    @Override
    @ApiOperation("查询预警信息违规详情分页列表")
    @SaCheckPermission("cre_warning_detail_list")
    public ResponseDTO list(Cre_warning_detailPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Cre_warning_detail> page = this.cre_warning_detailService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Cre_warning_detail cre_warning_detail : page) {
                cre_warning_detail.geneText();
                list.add(cre_warning_detail);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看预警信息违规详情")
    @SaCheckPermission("cre_warning_detail_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Cre_warning_detail cre_warning_detail  = cre_warning_detailService.getById(id);
        if (cre_warning_detail != null) {
            cre_warning_detail.geneText();
        }
        return ResponseDTO.succData(cre_warning_detail);
    }

    @Override
    @ApiOperation("删除预警信息违规详情")
    @SaCheckPermission("cre_warning_detail_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        cre_warning_detailService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改预警信息违规详情")
    @SaCheckPermission("cre_warning_detail_save")
    public ResponseDTO save(Cre_warning_detail cre_warning_detail) {
        Assert.notNull(cre_warning_detail, "cre_warning_detail不能为空");
        cre_warning_detailService.saveOrUpdate(cre_warning_detail);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="批量删除预警信息违规详情")
    @SaCheckPermission("cre_warning_detail_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        cre_warning_detailService.removeByIds(list);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("cre_warning_detail_list")
    public void output(HttpServletResponse response, Cre_warning_detailPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "预警信息违规详情";
        String[] heads = {"详情记录id","预警信息id","规则id","预警时间","预警地点","校验结果","单条描述","违规类型","规则名称","生成时间"};
        String[] fields = {"id","warningid","ruleid","warningtime","addr","validate","describe","type","rulename","createtime"};
        Page<Cre_warning_detail> page = this.cre_warning_detailService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }

    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "预警信息违规详情";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"详情记录id","预警信息id","规则id","预警时间","预警地点","校验结果","单条描述","违规类型","规则名称","生成时间"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }

	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","warningid","ruleid","warningtime","addr","validate","describe","type","rulename","createtime"};
        List<Cre_warning_detail> list = (List<Cre_warning_detail>)ExcelUtil.Import(fields, file, Cre_warning_detail.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.cre_warning_detailService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

}
